﻿<#
.SYNOPSIS  
    Removes web applications, IIS sites and associated content databases.  
.DESCRIPTION  
    
.NOTES
    
.LINK  
    http://7up.codeplex.com 
#>

Write-Host ("   Removing web applications ({0})..." -f [DateTime]::Now.ToString())

#add SharePoint snap in
if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

#get config
$config = [xml](Get-Content Upgrade.xml)

foreach($webApp in $config.Settings.WebApplications.WebApplication)
{
	if($webApp -ne $null)
	{
		if((Get-SPWebApplication $webApp.Name -ErrorAction SilentlyContinue) -ne $null)
		{
			Write-Host ("      Removing web application '{0}' ({1})..." -f $webApp.Name, [DateTime]::Now.ToString())
			Remove-SPWebApplication -Identity $webApp.Name -DeleteIISSite -RemoveContentDatabases -Confirm:$false
		}
		else
		{
			Write-Host ("      Skipping web application '{0}' because it does not exist ({1})." -f  $webApp.Name, [DateTime]::Now.ToString())
		}
	}
}